package  
{
	import com.greensock.easing.*;
	import com.greensock.loading.data.VideoLoaderVars;
	import org.flixel.*;
	import org.flixel.FlxState;
	import org.flixel.plugin.photonstorm.FlxBar;
	import flash.utils.getQualifiedClassName;
	import com.greensock.TweenLite;

	public class PlayState extends FlxState 
	{
		[Embed(source = "../assets/LevelOnel.png")] private var imgLevel:Class;
		[Embed(source = "../assets/Diamond.png")] private var imgDiamond:Class;
		[Embed(source = "../assets/LevelGraphics.png")] private var imgBG:Class;
		
		[Embed(source = "../assets/GandalfQuote.png")] private var imgQuote:Class;
		[Embed(source = "../assets/purplewin.png")] private var imgPurpleWin:Class;
		[Embed(source = "../assets/yellowwin.png")] private var imgYellowWin:Class;
		
		
		public var topMessage:FlxText;
		
		public var map:FlxTilemap;
		
		public var p1:SplashPlayer;
		public var p2:BlockyPlayer;
		public var p3:SplashPlayer;
		public var p4:BlockyPlayer;
		
		public var baseTeam1:FlxSprite;
		public var safeArea1:FlxSprite;
		public var baseTeam2:FlxSprite;
		public var safeArea2:FlxSprite;
		
		public var txtScore1:FlxText;
		public var txtScore2:FlxText;
		
		public var txtBlocks1:FlxText;
		public var txtBlocks2:FlxText;
		
		private var groupBases:FlxGroup;
		
		private var background:FlxSprite;
		
		private var cam:FlxCamera;
		public var gameSTarte:Boolean = false;
		
		private var scoreUI:ScoreUI;
		
		private var scorePause:Boolean = false;
		
		private var endScreen:Boolean = false;
		
		override public function create():void {
			
			super.create();
			
			groupBases = new FlxGroup();
			Registry.groupUI = new FlxGroup();
			
			background = new FlxSprite(0, -FlxG.height, imgBG);
			add(background);
			
			map = new FlxTilemap();
			map.loadMap(FlxTilemap.imageToCSV(imgLevel, false, 1), FlxTilemap.ImgAuto, 8, 8, FlxTilemap.AUTO, 0, 1, 1);
			map.visible = false;
/*			map.x = -400;
			map.y = -400;*/
			add(map);
			
			
			Registry.groupTemp = new FlxGroup();
			Registry.groupSplash = new FlxGroup();
			Registry.groupBlocks = new FlxGroup();
			Registry.groupPlayers = new FlxGroup();
			add(Registry.groupBlocks);
			add(groupBases);
			add(Registry.groupPlayers);
			
			p1 = new SplashPlayer(100, 100, 1, "UP", "DOWN", "LEFT", "RIGHT", "M", false);
			p2 = new BlockyPlayer(100,300, 1, "W", "S", "A", "D", "Q", "E", "Z", "TWO", false);
			p3 = new SplashPlayer(FlxG.width - 100-20, 100, 2, "T", "G", "F", "H", "R", true);
			p4 = new BlockyPlayer(FlxG.width - 100-20, 300, 2, "I", "K", "J", "L", "O", "U", "P", "THREE", true);
			
			Registry.groupPlayers.add(p1);
			Registry.groupPlayers.add(p2);
			Registry.groupPlayers.add(p3);
			Registry.groupPlayers.add(p4);
			
			add(Registry.groupTemp);
			add(Registry.groupSplash);
			
			
			
			txtScore1 = new FlxText(10, 20, 200, "RED: " + Registry.scoreTeam1);
			txtScore2 = new FlxText(FlxG.width - 210, 20, 200, "BLUE: " + Registry.scoreTeam2);
			
			txtScore1.setFormat(null, 32, 0xff0000, null, 0x010101);
			txtScore2.setFormat(null, 32, 0x0000ff, "right", 0x010101);
			//Registry.groupUI.add(txtScore1);
			//Registry.groupUI.add(txtScore2);
			
			txtBlocks1 = new FlxText(10, 320, 200, "RED BLOCKS LEFT: " + (p2.maxBlocks - p2.numBlocks));
			txtBlocks2 = new FlxText(FlxG.width - 230, 320, 220, "BLUE BLOCKS LEFT: " + (p4.maxBlocks - p4.numBlocks));
			
			txtBlocks1.setFormat(null, 16, 0xff0000, null, 0x010101);
			txtBlocks2.setFormat(null, 16, 0x0000ff, "right", 0x010101);
			//Registry.groupUI.add(txtBlocks1);
			//Registry.groupUI.add(txtBlocks2);
			
			//var p1bar:FlxBar = new FlxBar(10, 10, FlxBar.FILL_LEFT_TO_RIGHT, 100, 10, p1, "sprayMeter", 0, p1.sprayMaxMeter , false);
			//p1bar.createFilledBar(0xff000000, 0xffff00ff, false, 0xffffffff);
			//var p3bar:FlxBar = new FlxBar(FlxG.width-100-10, 10, FlxBar.FILL_LEFT_TO_RIGHT, 100, 10, p3, "sprayMeter", 0, p3.sprayMaxMeter , false);
			//p3bar.createFilledBar(0xff000000, 0xffff00ff, false, 0xffffffff);			
			//Registry.groupUI.add(p3bar);
			//Registry.groupUI.add(p1bar);
			//Registry.groupUI.add(new SplashPlayerUI(10, 10, p1));
			//Registry.groupUI.add(new SplashPlayerUI(FlxG.width-100-10, 10, p3));
			add(Registry.groupUI);
			
			setupBasesAndSafeAreas();
			
			cam = new FlxCamera(0, 0, FlxG.width, FlxG.height);
			cam.scroll.y = -FlxG.height;
			TweenLite.to(cam.scroll, 4, { y : -2 , ease : Quad.easeInOut, onComplete : StartGame});
			FlxG.addCamera(cam);
			
			scoreUI = new ScoreUI(this);
			add(scoreUI);
			
			HideUI();
		}
		
		private function setupBasesAndSafeAreas():void {
			
			baseTeam1 = new FlxSprite(90, 50, imgDiamond);
			baseTeam2 = new FlxSprite(FlxG.width - 120, 50, imgDiamond);
			
			baseTeam1.ID = 1;
			baseTeam2.ID = 2;
			
			baseTeam1.color = 0xeef43f;
			baseTeam2.color = 0x6b3d99;
			
			baseTeam1.width = 26;
			baseTeam1.height = 16;
			baseTeam1.centerOffsets();
			
			baseTeam2.width = 26;
			baseTeam2.height = 16;
			baseTeam2.centerOffsets();
			
			safeArea1 = new FlxSprite(baseTeam1.x - 56, baseTeam1.y - 60);
			safeArea1.makeGraphic(140, 100, 0x66eef43f);
			safeArea1.visible = false;
			
			safeArea2 = new FlxSprite(baseTeam2.x - 60, baseTeam2.y - 60);
			safeArea2.makeGraphic(140, 100, 0x666b3d99);
			safeArea2.visible = false;
			
			Registry.safeArea1 = safeArea1;
			Registry.safeArea2 = safeArea2;
			
			groupBases.add(safeArea1);
			groupBases.add(safeArea2);
			
			groupBases.add(baseTeam1);
			groupBases.add(baseTeam2);
		}
		
		private function HideUI():void
		{
			for each (var o:FlxSprite in Registry.groupUI.members) 
			{
				o.alpha = 0;
			}
		}
		
		private function StartGame():void
		{
			for each (var o:FlxSprite in Registry.groupUI.members) 
			{
				TweenLite.to(o, 2, { alpha:1 } );
			}			
			p1.flicker(0.5);
			p2.flicker(0.5);
			p3.flicker(0.5);
			p4.flicker(0.5);
			gameSTarte = true;
			TweenLite.to(new Object(), 2, { onComplete : scoreUI.UpdateOnce } );
		}
		
		public function particleOverlap(pushed:FlxSprite, particle:SplashParticle):void {
			
			if (pushed != particle.owner) {
				
				//if (pushed == baseTeam1) {
				
				if (getQualifiedClassName(pushed) == "Block") {
					
					if ((pushed as Block).type == "mult") {
						
						particle.owner = (pushed as Block);
						
						particle.lifespan = 0.5;
						
						if (Math.random() < 0.5) {
							particle.velocity.y = Math.random() * 100 + 100;
						}
						else {
							particle.velocity.y = Math.random() * -100 - 100;
						}
						
						
						if (Math.random() < 0.5) {
							particle.velocity.x = Math.random() * 100 + 100;
						}
						else {
							particle.velocity.x = Math.random() * -100 - 100;
						}
					}
					
					
					pushed.health -= 0.21;
					pushed.flicker(0.05);
					
					if (pushed.health <= 0) {
						TweenLite.to(pushed, 0.3, { alpha:0, onComplete: killBlock, onCompleteParams:[pushed]} );
						
					}
					
					
				}
				else {
					pushed.velocity.x += particle.velocity.x / 10;
					pushed.velocity.y += particle.velocity.y/ 10;
					
				}
			}
			
		}
		
		private function killBlock(pushed:Block):void {
			var auxPlayer:BlockyPlayer = (pushed as Block).owner;
			auxPlayer.numBlocks++;
			pushed.kill();
		}
		
		public function collidePlayersAndBlocks(player:FlxSprite, block:Block):void {
			
			if (block.type == "mine") {	
				
				switch(int(Math.random() * 4))
				{
					case 0:
						FlxG.play(Registry.spikedeath1snd);
						break;
					case 1:
						FlxG.play(Registry.spikedeath2snd);
						break;
					case 2:
						FlxG.play(Registry.spikedeath3snd);
						break;
					case 3:
						FlxG.play(Registry.spikedeath4snd);
						break;						
				}
				
				if (player.ID == 1) {
					player.x = baseTeam1.x;
					player.y = baseTeam1.y;	
					
				}
				else if (player.ID == 2) {
					player.x = baseTeam2.x;
					player.y = baseTeam2.y;
				}
				
				player.flicker(0.5);
			}
			else if(block.type == "wall") {
				FlxObject.separate(player, block);
			}
			
		}
		
		public function playerOverlap(p1:FlxSprite, p2:FlxSprite):void {
			
			if (p1!= p2) {
				FlxObject.separate(p1, p2);
			}
		}	
		
		public function processScore(base:FlxSprite, player:FlxSprite):void {
			
			
			
			if (base.ID == 2 && player.ID == 1 && !scorePause) {
				FlxG.play(Registry.diamondsnd);
				scorePause = true;
				
				Registry.scoreTeam1++;
				
				
				
				if (Registry.scoreTeam1 >= Registry.maxScore) {
					//FlxG.switchState(new GameOverState);
					DoGameOver();
				}
				else {
					p1.x = 100; p1.y = 100; p1.flicker(1);
					p2.x = 100; p2.y = 200; p2.flicker(1);
					p3.x = FlxG.width - 100; p3.y = 100; p3.flicker(1);
					p4.x = FlxG.width - 100; p4.y = 200; p4.flicker(1);
					FlxG.flash(0xaa6a21a5, 0.5);
				}
			}
			else if (base.ID == 1 && player.ID == 2 && !scorePause) {
				FlxG.play(Registry.diamondsnd);
				Registry.scoreTeam2++;
				
				if (Registry.scoreTeam2 >= Registry.maxScore) {
					//FlxG.switchState(new GameOverState);
					DoGameOver();
				}
				else {
					p1.x = 100; p1.y = 100; p1.flicker(1);
					p2.x = 100; p2.y = 200; p2.flicker(1);
					p3.x = FlxG.width - 100; p3.y = 100; p3.flicker(1);
					p4.x = FlxG.width - 100; p4.y = 200; p4.flicker(1);
					FlxG.flash(0xaaffff00, 0.5);
				}
			}
		}
		
		private function DoGameOver():void
		{
			gameSTarte = false;
			p1.x = 100; p1.y = 100; p1.flicker(100);
			p2.x = 100; p2.y = 200; p2.flicker(100);
			p3.x = FlxG.width - 100; p3.y = 100; p3.flicker(100);
			p4.x = FlxG.width - 100; p4.y = 200; p4.flicker(100);			
			
			if (Registry.scoreTeam1 > Registry.scoreTeam2)
			{
				TweenLite.to(p3, 2, { alpha:0 } );
				TweenLite.to(p3.sprayBar, 2, { alpha:0 } );
				TweenLite.to(p4, 2, { alpha:0 } );
				TweenLite.to(p4.blockBar, 2, { alpha:0 } );
				
				
			}
			else
			{
				TweenLite.to(p1, 2, { alpha:0 } );
				TweenLite.to(p1.sprayBar, 2, { alpha:0 } );
				TweenLite.to(p2, 2, { alpha:0 } );
				TweenLite.to(p2.blockBar, 2, { alpha:0 } );
			}
			
			TweenLite.to(new Object(), 1, { onComplete :DropFinishText } );
		}
		private function DropFinishText():void
		{
			endScreen = true;
			var back:FlxSprite = new FlxSprite(0, -2);
			back.makeGraphic(FlxG.width, FlxG.height, 0xbbffffff);
			back.alpha = 0;
			TweenLite.to(back, 1, { alpha:1 } );
			Registry.groupUI.add(back);
			var quote:FlxSprite = new FlxSprite(0, 0, imgQuote);
			quote.y= -FlxG.height;
			Registry.groupUI.add(quote);
			TweenLite.to(quote, 1, { y:0 , ease : Elastic.easeInOut } );
			
			if (Registry.scoreTeam1 > Registry.scoreTeam2)
			{
				var win:FlxSprite = new FlxSprite(0, 0, imgYellowWin);
				win.y = -FlxG.height + 150;
				Registry.groupUI.add(win);
				TweenLite.to(win, 1, { y:150 , ease : Elastic.easeInOut } );
				FlxG.play(Registry.winYellowSnd);
			}
			else
			{
				var win:FlxSprite = new FlxSprite(0, 0, imgPurpleWin);
				win.y = -FlxG.height + 150;
				Registry.groupUI.add(win);
				TweenLite.to(win, 1, { y:150 , ease : Elastic.easeInOut } );
				FlxG.play(Registry.winPurpleSnd);
			}
			
		}
		
		
		
		
		override public function update():void {
			
			if (scorePause) scorePause = false;
			
			super.update();
			
			
			// collisions
			FlxG.overlap(Registry.groupPlayers, Registry.groupSplash, particleOverlap);
			FlxG.overlap(Registry.groupBlocks, Registry.groupSplash, particleOverlap);
			
			FlxG.overlap(Registry.groupPlayers, Registry.groupBlocks, collidePlayersAndBlocks);			
			FlxG.collide(Registry.groupBlocks, Registry.groupBlocks);			
			
			if (FlxG.keys.justPressed("ZERO")) FlxG.switchState(new PlayState());
			
			FlxG.overlap(Registry.groupPlayers, Registry.groupPlayers, playerOverlap);
			
			FlxG.collide(Registry.groupPlayers, map);
			FlxG.collide(Registry.groupBlocks, map);
			
			FlxG.collide(groupBases, map);
			FlxG.overlap(groupBases, Registry.groupPlayers, processScore);
			
			
			// ui
			txtBlocks1.text = "RED BLOCKS LEFT: " + p2.numBlocks;
			txtBlocks2.text = "BLUE BLOCKS LEFT: " + p4.numBlocks;
			
			txtScore1.text = "RED: " + Registry.scoreTeam1;
			txtScore2.text = "BLUE: " + Registry.scoreTeam2;
			
			safeArea1.visible = safeArea1.flickering;
			safeArea2.visible = safeArea1.flickering;
			scoreUI.UpdateOnce();
			
			// moves player bars with them
			p1.sprayBar.x = p1.x-3;
			p1.sprayBar.y = p1.y - 9;
			
			p2.blockBar.x = p2.x-1;
			p2.blockBar.y = p2.y - 7;
			
			p3.sprayBar.x = p3.x-3;
			p3.sprayBar.y = p3.y - 9;
			
			p4.blockBar.x = p4.x-1;
			p4.blockBar.y = p4.y - 7;
			
			
			if (!p1.onScreen()) {
				p1.x = baseTeam1.x;
				p1.y = baseTeam1.y;
			}
			
			if (!p2.onScreen()) {
				p2.x = baseTeam1.x;
				p2.y = baseTeam1.y;
			}
			
			if (!p3.onScreen()) {
				p3.x = baseTeam2.x;
				p3.y = baseTeam2.y;
			}
			
			if (!p4.onScreen()) {
				p4.x = baseTeam2.x;
				p4.y = baseTeam2.y;
			}
			
			if (FlxG.keys.justPressed("ONE") && endScreen) {
				
				FlxG.switchState(new MenuState);
				
			}
			
		}
	}

}